﻿using Furion.LinqBuilder;
using JQData.Core.DbModel;
using JQData.Core.TushareSDK.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace JQData.Core.TushareSDK
{
    public class StockApi
    {
        private readonly TushareCore _core;

        public StockApi(TushareCore core)
        {
            _core = core;
        }

        public async Task<List<DailyResult>> GetDailyAsync(string tsCode, DateOnly start, DateOnly? end = null)
        {
            var parameters = new Dictionary<string, object>() {
                { "ts_code",tsCode},
                
            };

            if (end == null)
            {
                parameters.Add("trade_date", start.ToString("yyyyMMdd"));
            }
            else {
                parameters.Add("start_date", start.ToString("yyyyMMdd"));
                parameters.Add("end_date", end.Value.ToString("yyyyMMdd"));
            }



            var items = await _core.CallApiAsync<List<JsonElement>>("daily", parameters);
            var list = new List<DailyResult>();

            foreach (var item in items.Data.Items)
            {
                var t = item[2].GetDecimal();
                var sr = new DailyResult()
                {
                    TsCode = item[0].ToString(),
                    TradeDate = DateOnly.ParseExact(item[1].ToString(), "yyyyMMdd"),
                    Open = item[2].GetDecimal(),
                    High = item[3].GetDecimal(),
                    Low = item[4].GetDecimal(),
                    Close = item[5].GetDecimal(),
                    PreClose = item[6].GetDecimal(),
                    Change = item[7].GetDecimal(),
                    PctChange = item[8].GetDecimal(),
                    Vol = item[9].GetDecimal(),
                    Amount = item[10].GetDecimal()
                };
                list.Add(sr);
            }
            return list;

        }




        /// <summary>
        /// 获取游资分类名录信息
        /// </summary>
        /// <returns>游资名录列表</returns>
        public async Task<List<StockBaseResult>> GetStockBasicAsync(ListingStatus listingStatus = ListingStatus.上市)
        {
            var parameters = new Dictionary<string, object>();
            if (listingStatus == ListingStatus.暂停上市)
            {
                parameters.Add("list_status", "P");
            }
            if (listingStatus == ListingStatus.退市)
            {
                parameters.Add("list_status", "D");
            }

            //if (!string.IsNullOrEmpty(name))
            //{
            //    parameters.Add("name", name);
            //}

            var fields = "ts_code,symbol,name,area,industry,fullname,enname,cnspell,market,exchange,curr_type,list_status,list_date,delist_date,is_hs,act_name,act_ent_type";
            var items = await _core.CallApiAsync<List<string>>("stock_basic", parameters, fields);

            var list = new List<StockBaseResult>();

            foreach (var item in items.Data.Items)
            {
                var sr = new StockBaseResult()
                {
                    TsCode = item[0],
                    Code = item[1],
                    Name = item[2],
                    Area = item[3],
                    Industry = item[4],
                    FullName = item[5],
                    EnName = item[6],
                    Cnspell = item[7],
                    Market = item[8],
                    Exchange = item[9],
                    CurrType = item[10],
                    ListingStatus = item[11],
                    ListingDate = DateOnly.ParseExact(item[12].ToString(), "yyyyMMdd"),
                    DelistDate = item[13] == null ? null : DateOnly.ParseExact(item[13].ToString(), "yyyyMMdd"),
                    IsHS = item[14],
                    ActEntType = item[15],
                    ActName = item[16]
                };
                list.Add(sr);
            }
            return list;
        }


    }
}
